﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Warehouse_Management.New;

namespace Warehouse_Management
{
    public partial class frmXuatManager : DevExpress.XtraEditors.XtraForm
    {
        public frmXuatManager()
        {
            InitializeComponent();
        }
        public int status = 0;
        public List<PhieuGD> listPhieuGD = new List<PhieuGD>();
        public DataTable dt = new DataTable();
        private void frmNhapManager_Load(object sender, EventArgs e)
        {
            loadData();
        }
        public void loadData()
        {
            if ((TrangThaiChungTu)this.Tag == TrangThaiChungTu.PhieuXuat)
                dt = PhieuGD.SelectDynamic("LoaiPhieu = 'X'", null).Tables[0];
            else if ((TrangThaiChungTu)this.Tag == TrangThaiChungTu.PhieuNhap)
                dt = PhieuGD.SelectDynamic("LoaiPhieu = 'N'", null).Tables[0];
            gridControl1.DataSource = dt;
            try
            {
                gridControl1.Refresh();
                gridView1.RefreshData();
            }
            catch
            {
                gridControl1.Refresh();
            }

        }
        public void TaoMoi()
        {
            frmPhieuGiaoDich fN = new frmPhieuGiaoDich();
            if ((TrangThaiChungTu)this.Tag == TrangThaiChungTu.PhieuXuat)
                fN.Text = "PHIẾU XUẤT";
            else
                fN.Text = "PHIẾU NHẬP";
            fN.Tag = this.Tag;
            fN.ShowDialog(this);
            loadData();
        }
        public void seach(string condition)
        {
            string quyery = condition;
            if ((TrangThaiChungTu)this.Tag == TrangThaiChungTu.PhieuNhap)
                quyery = "LoaiPhieu = 'N' " + condition;
            else
                quyery = "LoaiPhieu = 'X' " + condition;
            //listPhieuGD = PhieuGD.LoadAll(quyery, "NgayNhap desc");
            dt = PhieuGD.SelectDynamic(quyery, "NgayNhap desc").Tables[0];
            gridControl1.DataSource = dt;
            try
            {
                gridControl1.Refresh();
                gridView1.RefreshData();
            }
            catch
            {
                gridControl1.Refresh();
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hit = gridView1.CalcHitInfo((e as MouseEventArgs).Location);
            if (hit.InRow)
            {
                gridView1.SelectRow(hit.RowHandle);
                this.EditPhieu();
            }
        }

        public void EditPhieu()
        {
           int RowHandle = gridView1.GetSelectedRows()[0];
           if (RowHandle >= 0)
           {
               int index = gridView1.GetDataSourceRowIndex(RowHandle);
               PhieuGD pgd = PhieuGD.Load((long)dt.Rows[index]["ID"]);

               if (pgd != null)
               {
                   frmPhieuGiaoDich f = new frmPhieuGiaoDich();
                   f.PGD = pgd;
                   f.Tag = this.Tag;
                   f.ShowDialog(this);
                   loadData();
               }
           }
        }
        private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.ToString() == "Trạng thái")
            {
                if (string.IsNullOrEmpty(e.DisplayText) || e.DisplayText == "1")
                    e.DisplayText = "Chưa thanh khoản";
                else
                    e.DisplayText = "Đã thanh khoản";
            }
        }
        public void delete()
        {
            if (MessageBox.Show("Bạn có muốn xóa các hàng này", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;
            int[] listdelete =  this.gridView1.GetSelectedRows();
            foreach (int i in listdelete)
            {
                long id = (long)dt.Rows[(gridView1.GetDataSourceRowIndex(i))]["ID"];
                PhieuGD.DeleteFull(id);
            }
        }
    }
}